package com.ruoyi.project.jxc.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jxc.domain.JxcDrug;
import com.ruoyi.project.jxc.domain.JxcMf;
import com.ruoyi.project.jxc.domain.JxcSup;
import com.ruoyi.project.jxc.service.IJxcSupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jxc/sup")
public class JxcSupController extends BaseController {
    @Autowired
    private IJxcSupService supService;

    @PreAuthorize("@ss.hasPermi('jxc:sup:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(JxcSup sup)
    {
        List<JxcSup> jxcSups = supService.selectSupList(sup);
        return getDataTable(jxcSups);
    }


    @PreAuthorize("@ss.hasPermi('jxc:sup:list')")
    @GetMapping("/list")
    public TableDataInfo list(JxcSup sup)
    {
        startPage();
        List<JxcSup> jxcSups = supService.selectSupList(sup);
        return getDataTable(jxcSups);
    }

    /**
     * 新增厂家
     */
    @PreAuthorize("@ss.hasPermi('jxc:sup:add')")
    @Log(title = "厂家管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody JxcSup sup)
    {
        if (UserConstants.NOT_UNIQUE.equals(supService.checkSupNameUnique(sup)))
        {
            return AjaxResult.error("新增供应商'" + sup.getSupName() + "'失败，供应商名称已存在");
        }
        sup.setCreateBy(SecurityUtils.getUsername());
        return toAjax(supService.insertSup(sup));
    }

    @PreAuthorize("@ss.hasPermi('jxc:sup:edit')")
    @GetMapping(value = "/{supId}")
    public AjaxResult getInfo(@PathVariable Long supId)
    {
        return AjaxResult.success(supService.selectSupById(supId));
    }

    @PreAuthorize("@ss.hasPermi('jxc:sup:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody JxcSup sup)
    {
        if (UserConstants.NOT_UNIQUE.equals(supService.checkSupNameUnique(sup)))
        {
            return AjaxResult.error("修改供应商'" + sup.getSupName() + "'失败，供应商名称已存在");
        }
        sup.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(supService.updateSup(sup));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('jxc:sup:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supId}")
    public AjaxResult remove(@PathVariable Long[] supId)
    {

        return toAjax(supService.deleteSupById(supId));
    }
}
